//
//  mygltexture.m
//  myfistopengl
//
//  Created by huiti123 on 2017/3/30.
//  Copyright © 2017年 wtw. All rights reserved.
//

#import "mygltexture.h"
#import <OpenGLES/ES2/gl.h>

#import "glutils.h"


static const char vertexShaderStr[] = GLES_STRING(
                                                  
attribute vec4 vPosition;
                                                  attribute vec2 textureCoords;
                                                  varying vec2 textureCoordsOut;

void main(void)
{
    //gl_Position = projection * modelView * vPosition;
    
    gl_Position = vPosition;
    gl_PointSize = 10.0;
    textureCoordsOut = textureCoords;
}
                                                  
);

static const char fragmentShaderStr[] = GLES_STRING(
                                                    
                                                    precision mediump float;
                                                    
                                                    uniform  sampler2D texture;
                                                    varying vec2 textureCoordsOut;
                                                    
                                                    void main()
{
    vec4 mask = texture2D(texture,textureCoordsOut);
    gl_FragColor = vec4(mask.rgb,1.0);
    
    //gl_FragColor = fragColor;
}
  );
                                                    
                                                    

@implementation mygltexture{
    
    GLuint _position;
    GLuint _texture;
    GLuint _textureCoords;
}



-(GLuint ) getTextureWithImage:(UIImage*)image{
    CGImageRef  cgImageRef = [image CGImage];
    
    GLuint width =(GLuint) CGImageGetWidth(cgImageRef);
    GLuint  height  = (GLuint) CGImageGetHeight(cgImageRef);
    
    CGRect  rect = CGRectMake(0, 0, width, height);
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    void *imageData = malloc(width *height *4);
    CGContextRef context = CGBitmapContextCreate(imageData, width, height, 8, width*4, colorSpace, kCGImageAlphaPremultipliedLast|kCGImageByteOrder32Big);
    
    CGColorSpaceRelease(colorSpace);
    CGContextTranslateCTM(context, 0, height);
    CGContextScaleCTM(context, 1.0f, -1.0f);
    
    CGContextClearRect(context, rect);
    CGContextDrawImage(context, rect, cgImageRef);
    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    
    glEnable(GL_TEXTURE_2D);
    GLuint _textureId;
    glGenTextures(1, &_textureId);
    glBindTexture(GL_TEXTURE_2D, _textureId);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
                 width, height, 0, GL_RGBA,
                 GL_UNSIGNED_BYTE, imageData);
    
    glBindTexture(GL_TEXTURE_2D, 0);
    CGContextRelease(context);
    free(imageData);
    
    return _textureId;
}


-(void) setupGlParamenter:(GLuint)program{
    _position = glGetAttribLocation(program, "vPosition");
    _texture = glGetAttribLocation(program, "texture");
    _textureCoords = glGetAttribLocation(program, "textureCoordsOut");
    
    glActiveTexture(GL_TEXTURE_2D);
    
    GLuint textureId = [self getTextureWithImage:[UIImage imageNamed:@"ffmpeg"]];
    glBindTexture(GL_TEXTURE_2D, textureId);
    glUniform1i(_texture, 1);
    
    const GLfloat texCoords[]={
        0,0,
        1,0,
        0,1,
        1,1,
    };
    glVertexAttribPointer(_texture, 2, GL_FLOAT, GL_FALSE, 0, texCoords);
    glEnableVertexAttribArray(_texture);
    
    const  CGFloat vertices []={
        -1,-1,0,
        1,-1,0,
        -1,1,0,
        1,1,0,
        
    };
    glVertexAttribPointer(_position, 3, GL_FLOAT, GL_FALSE, 0, vertices);
    glEnableVertexAttribArray(_position);
    
}

-(void) drawElements{
 
    const GLubyte indices[]={
        0,1,2,1,2,3,
    };
    glDrawElements(GL_TRIANGLES, sizeof(indices)/sizeof(GLubyte), GL_UNSIGNED_BYTE, indices);
}


-(GLuint) getProgram
{
    GLuint  _program = [glutils loadProgram:[NSString stringWithUTF8String:vertexShaderStr] withFragmentShaderStr:[NSString stringWithUTF8String:fragmentShaderStr]];
    if(_program == 0){
        return 0;
    }
    
    return _program;
}

@end
